//
//  SFPCheckAddResultView.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import UIKit
import ProgressHUD
class SFPCheckAddResultView: UIView {
    @IBOutlet var bgView: UIView!
    @IBOutlet var nameLabel: UILabel!
    @IBOutlet var locationLabel: UILabel!
    @IBOutlet var doctorLabel: UILabel!
    @IBOutlet var resultTV: UITextView!
    var addResultBlock: (() -> Void)?
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    override func awakeFromNib() {
        super.awakeFromNib()
        self.resultTV.placeholder = "Please enter result"
        self.resultTV.placeholderColor = .lightGray
    }
    var model: SFPCheckAppointmentModel? {
        didSet {
            self.nameLabel.text = self.model?.name
            self.resultTV.text = self.model?.result
            self.locationLabel.text = "Location: " + self.model!.location
            self.doctorLabel.text = "Appointment: " + self.model!.doctor
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.bgView.addRoundCorners(topLeftRadius: 35, topRightRadius: 35, bottomLeftRadius: 0, bottomRightRadius: 0, borderWidth: 0, borderColor: .red)
    }
    
    @IBAction func clickOkAction(_ sender: Any) {
        if self.resultTV.text.isEmpty {
            ProgressHUD.showError("Please enter result")
            return
        }
        self.model?.result = self.resultTV.text
        self.model?.completed = true
        self.model?.updateToDB()
        self.addResultBlock?()
        ProgressHUD.showSuccess("Add result succeed")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.removeFromSuperview()
        }
    }
    @IBAction func clickCancelAction(_ sender: Any) {
        self.removeFromSuperview()
    }
}
